﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using Swaf;
using Swaf.Gui;

using Swaf.BizObj;
using Swaf.Container;
using Swaf.Security;

namespace GoogleMap.Gui
{
	public class GoogleMapBizProc : BaseBusinessProcess
	{
		public GoogleMapBizProc(ActiveBizProc procState)
			: base(procState)
		{
		}

		protected override void setupRuleNames()
		{
		}

		public override Privileges CurrentEditPrivilege
		{
			get
			{
				Privileges priv = Privileges.Add;
				return priv;
			}
		}

		protected override void showBizObjListInDisplay(IBizObjListDisplay disp, IBizObjCursor cur)
		{
			if (disp is IMapDisplay)
			{
				if (State.ParentBizProcs.ContainsKey("mapPin"))
				{
					List<ParentBizProcInfo> piss = State.ParentBizProcs["mapPin"];
					IBizObjList[] lists = new IBizObjList[piss.Count];
					for(int c = 0; c < lists.Length; c++)
						lists[c] = m_procState.BPC.getActiveBizProc(piss[c].ParentId).BizProc.SourceBizObjs;
					((IMapDisplay)disp).setupMapIcons(lists);
				}
			}
			base.showBizObjListInDisplay(disp, cur);
		}

		public override IBizObj selectBizObj(object keyValue, object source)
		{
			string[] pieces = keyValue.ToString().Split(',');
			string meta = pieces[0];
			string parentId = pieces[1];
			if (State.ParentBizProcs.ContainsKey("dataSource"))
			{
				List<ParentBizProcInfo> piss = State.ParentBizProcs["dataSource"];
				foreach(ParentBizProcInfo p in piss)
					if (p.Meta.CompareTo(meta) == 0)
					{
						m_procState.BPC.getActiveBizProc(p.ParentId).BizProc.selectBizObj(parentId, null);
						break;
					}
			}

			return null;
			//return base.selectBizObj(keyValue, source);
		}

		protected override void getBizObjList()
		{
			if (State.ParentBizProcs.ContainsKey("dataSource"))
			{
				List<ParentBizProcInfo> piss = State.ParentBizProcs["dataSource"];
				m_bizobjs = App.bizObjMgr.createList("MapPoint");
				foreach(ParentBizProcInfo pinfo in piss)
				{
					bool skipThis = false;
					if (m_filterSortCriteria != null && !Cmn.IsEmpty(pinfo.Meta))
					{
						skipThis = !Cmn.IsTrue(m_filterSortCriteria.get("filterOptions.(field:" + pinfo.Meta + ").value"));
					}
					if (!skipThis)
					{
						ActiveBizProc abp = m_procState.BPC.getActiveBizProc(pinfo.ParentId);
						IBizObjList plist = abp.BizProc.SourceBizObjs;
						if (plist != null)
							foreach (IBizObj addr in plist)
							{
								IBizObj mapItem = null;
								if (!Cmn.IsEmpty(pinfo.ParentSelectedBizObjFieldName))
								{
									object child = addr.get(pinfo.ParentSelectedBizObjFieldName);
									if (child is IBizObjList)
										foreach (IBizObj childAddr in (IBizObjList)child)
											mapItem = addToList(childAddr, addr);
									else if (child is IBizObj)
										mapItem = addToList((IBizObj)child, addr);
								}
								else
									mapItem = addToList(addr, null);
								if (mapItem != null)
									mapItem.put("meta", pinfo.Meta);
							}
					}
				}
				m_retrievedBizObjList = true;
			}
			
		}

		protected IBizObj addToList(IBizObj addr, IBizObj parent)
		{
			IBizObj mi = m_bizobjs.insertNew();
			mi.put("parent", parent);
			mi.put("location", addr);
			return mi;
		}
	}
}
